﻿package ygg.encoding.amf
{
	import j.sus2maj7.encoding.*;
	import flash.utils.ByteArray;
	
	public class AmfEncoding extends AbstractEncoding
	{
		private var _header:AmfHeader;
		private var _body:Object;
		
		
		public function AmfEncoding()
		{
			super();
		}
		
		public function get header():AmfHeader { return _header; }
		
		public function set header(value:AmfHeader):void
		{
			_header = value;
		}
		
		public function get body():Object { return _body; }
		
		public function set body(value:Object):void
		{
			_body = value;
		}
		
		
		override public function toString():String
		{
			return "Amf: \n" + _header.toString();
		}
		
		override public function setBytes(ba:ByteArray):void
		{
			this.binary = new AmfBinary(ba);
			
		}
		
		override public function decode(context:IContext=null):void
		{
			if (!codec) codec =  new AmfCodec();
			this.object = codec.decode(binary);
			
			_header = Amf(this.object).header;
			_body = Amf(this.object).body;
		}
		
		override public function encode(context:IContext=null):void
		{
			if (!codec) codec =  new AmfCodec();
			//*
			var ao:Amf = new Amf();
			ao.header = _header;// new AmfHeader('dfgdfg', 3, 0);
			ao.body = _body;
			//*/
			binary = codec.encode(ao);
			
		}
		
		public function addObject(key:String, value:*):void
		{
			_body[key] = value;
		}
	}
}